package com.museum.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.museum.base.BaseResponse;
import com.museum.controller.APi.feignAPI.FeignAPIController;
import com.museum.core.utils.VeDate;
import com.museum.entity.DO.Clue;
import com.museum.entity.DO.ColProcess;
import com.museum.entity.DTO.CommitCol;
import com.museum.entity.DTO.QueryCol;
import com.museum.entity.DTO.QueryPrice;
import com.museum.mapper.*;
import io.swagger.annotations.ApiModelProperty;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author Ruiwen
 * @since 2021-01-05
 */
@Service
public class PriceServiceImpl extends ServiceImpl<ClueMapper, Clue> {
     @Autowired
     private FeignAPIController feignAPIController;
     @Autowired
     private ProcessMapper processService;
     @Autowired
     private ClueMapper clueMapper;
     @Autowired
     private ProcessMapper processMapper;
     @Autowired
     private SortMapper sortMapper;
     @Autowired
     private YearMapper yearMapper;
     @Autowired
     private GradeMapper gradeMapper;
    /**
     * @param
     * @Desciption 条件查询征集
     * @return
     */
    public JSONObject getAllColByPage(long current, long limit, QueryPrice queryPrice){
        HashMap<String,Object> map =  new HashMap<String,Object>();
        List<Clue> clueList = new ArrayList<>();
        Integer id =null;
        String userName =null;
        String clueName = null;
        String way =null;
        String createTime =null;
        String receiveUser =null;
        if(queryPrice!=null){
             id = queryPrice.getId();
            userName = queryPrice.getUserName();
            clueName = queryPrice.getClueName();
            way = queryPrice.getWay();
            createTime = queryPrice.getCreateTime();
            receiveUser = queryPrice.getReceiveUser();
        }
        if (id == null || id == 0) {
            id = null;
        }
        System.out.println("id=="+id);
        if (!StringUtils.isEmpty(id)) {
            map.put("id",id);
        }
        if (!StringUtils.isEmpty(userName)) {
            map.put("userName",'%'+userName+'%');
        }
        if (!StringUtils.isEmpty(clueName)) {
            map.put("clueName",'%'+clueName+'%');
        }
        if (!StringUtils.isEmpty(way)) {
            map.put("way",'%'+way+'%');
        }
        if (!StringUtils.isEmpty(createTime)) {
            map.put("createTime",'%'+createTime+'%');
        }
        if (!StringUtils.isEmpty(receiveUser)) {
            map.put("receiveUser",'%'+receiveUser+'%');
        }
        map.put("operate_status",1);
        map.put("current", (current - 1) * limit);
        map.put("limit",limit);
        List<Clue> list = clueMapper.selectColByPrice(map);
        for (Clue clue : list) {
            int clueId = clue.getId();
            int sortsId = clue.getSortId();
            int yearId = clue.getYearId();
            int gradeId = clue.getGradeId();
            String sortName = sortMapper.selectNameById(sortsId);
            String yearName = yearMapper.selectNameById(yearId);
            String gradeName = gradeMapper.selectNameById(gradeId);
            clue.setSortName(sortName);
            clue.setYearName(yearName);
            clue.setGradeName(gradeName);
            Integer status = processMapper.getStatus(clueId);
            if(processMapper.isOperateStauts2(clueId)&&status!=-1&&processMapper.isPriceNotStauts(clueId)){
                clueList.add(clue);
            }
        }
        int count = clueMapper.selectCount(null);
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("total",count);
        jsonObject.put("ClueList",clueList);
        return jsonObject;
    }
    /**
     * @param
     * @Desciption 提交征集 状态转换为 1/-1
     * @return
     */
    public int commitCol(CommitCol commitcol,HttpServletRequest request){
        String token = request.getHeader("Authorization");
        BaseResponse<JSONObject> userMessageByUSerId = feignAPIController.getUserMessageByUSerId(token);
        JSONObject data = userMessageByUSerId.getData();
        String price_desc =  commitcol.getDesc();
       Integer status = commitcol.getStatus();
       Integer id = commitcol.getId();
       ColProcess process = clueMapper.selectProcessById(id);
        if(StringUtils.isEmpty(price_desc)){
            price_desc ="此人无留言.";
            process.setPriceDesc(price_desc);
        }else{
            process.setPriceDesc(commitcol.getDesc());
        }
        if(status==-1){
            process.setStatus(-1);
        }
       processMapper.commitColPrice(id,status);
       process.setPriceTime(VeDate.getStringDate());
       process.setPriceUser((String)data.get("realName"));
       return  processMapper.updateById(process);
    }

}
